package com.soft.service;

import java.math.BigDecimal;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.soft.entity.Money;
import com.soft.mapper.MoneyMapper;

@SpringBootTest
public class MoneyServiceTest {
@Autowired
private MoneyService moneyService;
@Autowired
private MoneyMapper moneyMapper;
@Test
public void queryAllTest() {
	List<Money> moneyList = moneyService.queryAll();
	Assert.notEmpty(moneyList, "为空");
	Assert.isTrue(moneyList.size()>0, "数据有值");
//	for(int i=0;i<moneyList.size();i++) {
//		System.out.println(moneyList.get(i).getNote());
//	}
}
//@Test
public void queryByIncomeTest() {
	List<Money> moneyList = moneyService.queryByIncome(BigDecimal.valueOf(145));
	Assert.notEmpty(moneyList, "根据income进行查询的数据不对");
}
// 测试新增功能
@Test
public void testAddMoney() {
    Money money = new Money();
    money.setIncome(new BigDecimal("500"));
    money.setSpend(new BigDecimal("200"));
    money.setNote("测试新增");
    money.setTotal(new BigDecimal("300"));

    int result = moneyService.addMoney(money);
    Assert.isTrue(result == 1, "新增失败");

    // 验证数据是否插入成功
    Money savedMoney = moneyMapper.selectById(money.getId());
    Assert.notNull(savedMoney, "新增数据未找到");
    Assert.isTrue(savedMoney.getIncome().compareTo(new BigDecimal("500")) == 0, "收入值不正确");
}

// 测试根据ID删除
@Test
public void testDeleteMoneyById() {
    // 先插入一条测试数据
    Money money = new Money();
    money.setIncome(new BigDecimal("100"));
    money.setSpend(new BigDecimal("50"));
    money.setTotal(new BigDecimal("50"));
    moneyMapper.insert(money);
    Long id = money.getId();

    // 执行删除
    int result = moneyService.deleteMoneyById(id);
    Assert.isTrue(result == 1, "删除失败");

    // 验证是否删除成功
    Money deletedMoney = moneyMapper.selectById(id);
    Assert.isNull(deletedMoney, "删除未生效");
}

// 测试条件删除
@Test
public void testDeleteMoneyByCondition() {
    // 插入两条测试数据
    Money money1 = new Money();
    money1.setIncome(new BigDecimal("200"));
    money1.setSpend(new BigDecimal("100"));
    money1.setTotal(new BigDecimal("100"));
    moneyMapper.insert(money1);

    Money money2 = new Money();
    money2.setIncome(new BigDecimal("300"));
    money2.setSpend(new BigDecimal("50"));
    money2.setTotal(new BigDecimal("250"));
    moneyMapper.insert(money2);

    // 删除总额小于等于200的记录
    int result = moneyService.deleteMoneyByCondition(new BigDecimal("200"));
    Assert.isTrue(result >= 1, "条件删除失败");

    // 验证剩余数据
    QueryWrapper<Money> qw = new QueryWrapper<>();
    qw.le("money_total", new BigDecimal("200"));
    List<Money> remaining = moneyMapper.selectList(qw);
    Assert.isTrue(remaining.isEmpty(), "条件删除未生效");
}

// 测试根据ID更新
@Test
public void testUpdateMoneyById() {
    // 先插入一条测试数据
    Money money = new Money();
    money.setIncome(new BigDecimal("600"));
    money.setSpend(new BigDecimal("300"));
    money.setTotal(new BigDecimal("300"));
    moneyMapper.insert(money);
    Long id = money.getId();

    // 执行更新
    money.setId(id);
    money.setSpend(new BigDecimal("200"));
    money.setTotal(new BigDecimal("400"));
    money.setNote("测试更新");
    int result = moneyService.updateMoneyById(money);
    Assert.isTrue(result == 1, "更新失败");

    // 验证更新结果
    Money updatedMoney = moneyMapper.selectById(id);
    Assert.isTrue(updatedMoney.getSpend().compareTo(new BigDecimal("200")) == 0, "更新金额不正确");
    Assert.isTrue(updatedMoney.getNote().equals("测试更新"), "备注更新不正确");
}

// 测试条件更新
@Test
public void testUpdateMoneyByIncome() {
    // 插入测试数据
    Money money = new Money();
    money.setIncome(new BigDecimal("800"));
    money.setSpend(new BigDecimal("300"));
    money.setTotal(new BigDecimal("500"));
    moneyMapper.insert(money);

    // 执行条件更新
    int result = moneyService.updateMoneyByIncome(new BigDecimal("800"), new BigDecimal("600"));
    Assert.isTrue(result == 1, "条件更新失败");

    // 验证更新结果
    QueryWrapper<Money> qw = new QueryWrapper<>();
    qw.eq("money_income", new BigDecimal("800"));
    Money updatedMoney = moneyMapper.selectOne(qw);
    Assert.isTrue(updatedMoney.getTotal().compareTo(new BigDecimal("600")) == 0, "总额更新不正确");
}
}
